﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Web.Security;

namespace StockShark.Models
{
    // konto maklerskie
    public class BrokerageAccount : IValidatableObject
    {
        public int Id { get; set; }
        [Required]
        [StringLength(64)]
        [DisplayName("Nazwa")]
        public string Name { get; set; }
        public decimal Balance { get; set; }
        public decimal TotalValue { get; set; }
        [DataType(DataType.Date)]
        [DisplayFormat(DataFormatString = "{0:d}")]
        public DateTime CreationDate { get; set; }
        public Guid UserId { get; set; }
        public virtual List<OwnedInstrument> OwnedInstruments { get; set; }
        public virtual List<ExecutedTransaction> ExecutedTransactions { get; set; }
        public virtual List<Order> Orders { get; set; }

        public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
        {
            List<BrokerageAccount> accountList = new StockSharkDB().BrokerageAccounts
                .Where(r => r.UserId == UserId && r.Id != Id)
                .ToList();
            if (accountList.Count >= 5)
                yield return new ValidationResult("Limit liczby otwartych rachunków został przekroczony.");
            foreach (var account in accountList)
            {
                if (account.Name == Name)
                    yield return new ValidationResult("Istnieje już rachunek o podanej nazwie.");
            }
        }
    }
}